package geektime.jdk.date;

import java.time.*;
import java.time.temporal.ChronoUnit;

/**
 * 演示Instant的用法
 */
public class InstantDemo {

    public static void main(String[] args) {
        instantUsing();
        localDateTimeUsing();
    }

    private static void localDateTimeUsing() {
        Instant instant = Instant.now();
        LocalDateTime localeNow = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        System.out.println("本地当前时间：" + localeNow);
        LocalDateTime beijingNow = LocalDateTime.ofInstant(instant, ZoneId.of("Asia/Shanghai"));
        System.out.println("北京当前时间：" + beijingNow);
        LocalDateTime tokyoNow = LocalDateTime.ofInstant(instant, ZoneId.of("Asia/Tokyo"));
        System.out.println("东京当前时间：" + tokyoNow);
        ZonedDateTime parisNow = instant.atZone(ZoneId.of("Europe/Paris"));
        System.out.println("巴黎当前时间：" + parisNow);
        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.UTC);
        System.out.println("当前UTC时间：" + offsetDateTime);
    }

    private static void instantUsing() {
        Instant instant = Instant.parse("2022-01-26T21:52:00.123Z");
        System.out.println("instant: " + instant);
        System.out.println("After 10 seconds: " + instant.plusSeconds(10));
        System.out.println("Before 10 seconds: " + instant.minusSeconds(10));
        System.out.println("After 5 minutes: " + instant.plus(5, ChronoUnit.MINUTES));
        System.out.println("Before 1 day: " + instant.minus(1, ChronoUnit.DAYS));
    }
}
